//
//  SearchViewController.m
//  RouteSearcher
//
//  Created by wenqingzhou on 2/11/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchResultViewController.h"
#import "SearchHistoryModel.h"

@implementation SearchViewController
@synthesize textOfTime,textOfTo,textOfFrom,segmentOfType,mySearchModel;

-(IBAction)useMyPostitionAsFrom:(UIButton *)sender
{
	self.textOfFrom.text=@"talonpojantie5";
	[self textFieldDidEndEditing:self.textOfFrom];
}
-(IBAction)useMyPostitionAsTo:(UIButton *)sender;
{
	self.textOfTo.text=@"talonpojantie5";
	[self textFieldDidEndEditing:self.textOfTo];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	if(textField==self.textOfTime)
	{
		NewDatePickerViewController *myDPVC = [[NewDatePickerViewController alloc] init];
		myDPVC.delegate = self;
		[self presentModalViewController:myDPVC animated:YES];
		[myDPVC release];
		return NO;
	}
	else
	{
		return YES;
	}
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
	if(textField != self.textOfTime && ![textField.text isEqual:@""])
	{
		NSString *tmpText=[[textField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"] retain];
		NSLog(@"text:%@",tmpText);
		[mySearchModel stringToGeocode:tmpText];
		[tmpText release];
		if(textField == self.textOfTo)
		{
			self.mySearchModel.fromOrTo=2;
		}
		else
		{
			self.mySearchModel.fromOrTo=1;
		}
		if([self.mySearchModel.tmp count]==0)
		{
			UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error!" message:@"Address not found!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
		else
		{
			ChooseAddressViewController *cavd = [[ChooseAddressViewController alloc] init];
			cavd.addressArray=self.mySearchModel.tmp;
			cavd.addressDelegate = self;
			[self presentModalViewController:cavd animated:YES];
			[cavd release];			
		}
		
	}
}
-(void)changeAddress:(NSInteger)index
{
	NSArray *tmpArray;
	if (self.mySearchModel.fromOrTo==1) {
		self.mySearchModel.arrayOfFromAddress=[self.mySearchModel.tmp retain];
		tmpArray=[[mySearchModel getFirstAddressFromTmpArrayObjectAtIndex:index] retain];
		mySearchModel.fromXCode=[tmpArray objectAtIndex:0];
		mySearchModel.fromYCode=[tmpArray objectAtIndex:1];	
		[tmpArray release];
		self.textOfFrom.text = [mySearchModel getAddressFromTmpArrayObjectAtIndex:index];
	}
	else {
		self.mySearchModel.arrayOfToAddress=[self.mySearchModel.tmp retain];
		tmpArray=[[mySearchModel getFirstAddressFromTmpArrayObjectAtIndex:index] retain];
		mySearchModel.toXCode=[tmpArray objectAtIndex:0];
		mySearchModel.toYCode=[tmpArray objectAtIndex:1];
		self.textOfTo.text = [mySearchModel getAddressFromTmpArrayObjectAtIndex:index];
		[tmpArray release];
	}
}
							 
- (void)changeDate:(NSDate *)newDate
{
	self.textOfTime.text = [self.mySearchModel updateDate:newDate];
	[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)newSearch:(UIButton *)sender
{
	NSURL *url=[[mySearchModel getRequestURLOfRoute] retain];
	if(url)
	{
		SearchResultViewController *myResultView=[[SearchResultViewController alloc] initWithStyle:UITableViewStyleGrouped];
		NSDate *now=[[NSDate alloc] init];
		NSDateFormatter *df= [[NSDateFormatter alloc] init];
		[df setDateFormat:@"HH:mm"];
		NSString *time=[[NSString alloc] initWithFormat:@"%@",[df stringFromDate:now]];						 
		myResultView.myRRRM = [RouteRequestResultModel initWithRequest:url andStart:self.textOfFrom.text andDest:self.textOfTo.text andSearchTime:time];
		[url release];
		[df release];
		[now release];
		[time release];
		if([myResultView.myRRRM getResultOfRequest]==YES)
		{
			[[SearchHistoryModel getSingleton] restore:myResultView.myRRRM];
		}
		[self.navigationController pushViewController:myResultView animated:YES];
		[myResultView release];
	}
}



// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	NSDate *now=[[NSDate alloc] init];
	[self.segmentOfType setTitle:@"Departure" forSegmentAtIndex:0];
	[self.segmentOfType setTitle:@"Arrival" forSegmentAtIndex:1];
	self.segmentOfType.segmentedControlStyle = UISegmentedControlStyleBar;
	self.mySearchModel = [searchModel initWithString:(self.segmentOfType.selectedSegmentIndex?@"2":@"1")];
	self.textOfTime.text = [self.mySearchModel updateDate:now];
	[now release];
    [super viewDidLoad];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[self.mySearchModel release];
	[self.textOfTime release];
	[self.textOfFrom release];
	[self.textOfTo release];
	[self.segmentOfType release];
    [super dealloc];
}


@end
